#!/usr/bin/python3
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support.select import Select
from PIL import Image
import os
import api
from ocr import Transform


URL="https://us.labcollector.online/apostlebio/extra_modules/lsmremote/index.php?action=preregister"

def screen_shot(driver):

    driver.save_screenshot("tmp.png")
    left_angle = driver.find_element_by_xpath("//div/p/img").location 
    #print(left_angle) 
    left = left_angle["x"] 
    top = left_angle["y"] 
    image = driver.find_element_by_xpath("//div/p/img") 
    width = image.size["width"]
    height = image.size["height"] 
    right = left+width 
    down = top+height 
    #print(left,top,right,down) 

    openim = Image.open("tmp.png")
    jietu = openim.crop((left,top,right,down)) 
    jietu.save("code.png") 

    
def Start_Fill(last_name, first_name, dob, gender, email_id, phone, address, city, live_in_los_angeles,
   state, zip_code, order_id, country='300', patient_race="Asian", patient_ethnicity="315"):
 
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get(URL)

    name = driver.find_element_by_id("name")
    patient_id = name.get_attribute('value')
    print(patient_id)
    screen_shot(driver)
    
    #first_name
    first_name_e = driver.find_element_by_id("first_name")
    ActionChains(driver).move_to_element(first_name_e).perform()
    first_name_e.send_keys(first_name)

    #last_name
    last_name_e = driver.find_element_by_id("last_name")
    ActionChains(driver).move_to_element(last_name_e).perform()
    last_name_e.send_keys(last_name)

    #gender
    gender_e = driver.find_element_by_id("gender")
    ActionChains(driver).move_to_element(gender_e).perform()
    Select(gender_e).select_by_visible_text(gender)
         
    #dob
    """
    js="document.getElementById('dob').removeAttribute('readonly')"
    #js = "$('input[id=dob]').removeAttr('readonly')"
    driver.execute_script(js)
    dob_e = driver.find_element_by_id("dob")
    ActionChains(driver).move_to_element(dob_e).perform()
    
    dob_e.click()
    dob_e.click()
    dob_e.send_keys(dob)
    """
    mm, dd, yyyy = dob.split('/')
    #this will display the dakepicker
    dob_e = driver.find_element_by_id("DATE_dob")
    dob_e.click()
    time.sleep(1)

    m_e = driver.find_element_by_class_name("monthselect")
    ActionChains(driver).move_to_element(m_e).perform()
    m_e.click()
    Select(m_e).select_by_value(str(int(mm) - 1))
    time.sleep(1)

    y_e = driver.find_element_by_class_name("yearselect")
    ActionChains(driver).move_to_element(y_e).perform()
    y_e.click()
    Select(y_e).select_by_value(yyyy)
    time.sleep(1)

    d_e = driver.find_element_by_xpath("//tbody/tr/td[contains(@class,'available') and text()='{}']".format(int(dd)))
    ActionChains(driver).move_to_element(d_e).perform()
    d_e.click()
    time.sleep(1)
    
    #email_id
    email_id_e = driver.find_element_by_id("email_id")
    ActionChains(driver).move_to_element(email_id_e).perform()
    email_id_e.send_keys(email_id)

    #phone
    phone_e = driver.find_element_by_id("phone")
    ActionChains(driver).move_to_element(phone_e).perform()
    phone_e.send_keys(phone)

    #address
    address_e = driver.find_element_by_id("address")
    ActionChains(driver).move_to_element(address_e).perform()
    address_e.send_keys(address)

    #city
    city_e = driver.find_element_by_id("city")
    ActionChains(driver).move_to_element(city_e).perform()
    city_e.send_keys(city)

    #live_in_los_angeles
    live_in_los_angeles_e = driver.find_element_by_id("live_in_los_angeles")
    ActionChains(driver).move_to_element(live_in_los_angeles_e).perform()
    Select(live_in_los_angeles_e).select_by_visible_text(live_in_los_angeles)

    #state
    state_e = driver.find_element_by_id("state")
    ActionChains(driver).move_to_element(state_e).perform()
    Select(state_e).select_by_visible_text(state)


    #zip_code
    zip_code_e = driver.find_element_by_id("zip_code")
    ActionChains(driver).move_to_element(zip_code_e).perform()
    zip_code_e.send_keys(zip_code)


    #country
    country_e = driver.find_element_by_id("country")
    ActionChains(driver).move_to_element(country_e).perform()
    Select(country_e).select_by_value(country)

    #patient_race
    patient_race_e = driver.find_element_by_id("patient_race")
    ActionChains(driver).move_to_element(patient_race_e).perform()
    Select(patient_race_e).select_by_visible_text(patient_race)

    #patient_ethnicity
    patient_ethnicity_e = driver.find_element_by_id("patient_ethnicity")
    ActionChains(driver).move_to_element(patient_ethnicity_e).perform()
    Select(patient_ethnicity_e).select_by_value(patient_ethnicity)


    #order_id
    order_id_e = driver.find_element_by_id("order_id")
    ActionChains(driver).move_to_element(order_id_e).perform()
    order_id_e.send_keys(order_id)


    data_protection_e = driver.find_element_by_id("data_protection")
    ActionChains(driver).move_to_element(data_protection_e).perform()
    data_protection_e.click()

    #Select(el).select_by_value('2')
    #select(el).select_by_visible_text('CA')

    #driver.find_element_by_id('su').click()
    Transform('code.png')
    code = api.Identify_Code('out.png')
    code_e = driver.find_element_by_id("captcha")
    ActionChains(driver).move_to_element(code_e).perform()
    code_e.click()
    code_e.send_keys(code)

    try:
        os.remove("tmp.png")
        os.remove("code.png")
        os.remove("out.png")
    except:
        pass
    
    time.sleep(320)
    driver.quit()

#sudo apt install tesseract-ocr
#sudo apt install libtesseract-dev
#sudo apt install python3-opencv
#sudo apt install python3-opencv
#sudo apt install python3-pil
#pip3 install pysseract

if __name__ == "__main__":
    data_dict = {
   'last_name': "Changcai",
   'first_name': "Xie",
   'dob': "08/19/1983",
   'gender':'M',
   'email_id': 'xiechc@gmail.com',
   'phone': '18910158363',
   'address': '35 Lane Apt 23',
   'city': "Walnut Creek",
   'live_in_los_angeles': "No",
   'state': 'CA',
   'zip_code': '100086',
   'order_id': "4444",

   'country':'300',
   'patient_race':"Asian",
   'patient_ethnicity':"315"
    }
    Start_Fill(**data_dict)


